package m4;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;

public class Producer {
    public static void main(String[] args) throws Exception {
        // 连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140"); // wht6.cn
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();

        // 定义交换机
        c.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);
        // 向交换机发送消息，并携带路由键
        while (true) {
            System.out.print("输入： ");
            String msg = new Scanner(System.in).nextLine();
            System.out.print("输入路由键： ");
            String key = new Scanner(System.in).nextLine();
            /*
            第二个参数： 路由键，和绑定键进行匹配，将消息路由到匹配的队列
            对于默认交换机这里用队列名来路由消息
             */
            c.basicPublish("direct_logs", key, null, msg.getBytes());
        }
    }
}
